import { observable, action } from "mobx";
import { WeaLocaleProvider } from "ecCom";
import { message, Modal } from 'antd';
import API from '../../apis';
import { showExpendError } from "../../util/cubeUtil";
const getLabel = WeaLocaleProvider.getLabel;
import {spellResourceCondition } from '../../util/index'
/**
 * 批量修改字段值
 */
export default class BatchEditStore {
    @observable form = {};
    @observable customid = {};
    @observable visible = false;
    @observable selectedRows: string[] = [];
    @observable dialog: any = {};
    @observable loading: boolean = false;
    pageexpandid: string = '';
    selectedRowKeys: any = [];
    @observable datas: any = [];
    @observable cells: any = [];
    constructor() {
        this.dialog = {
            loading: false,
            loadingTips: getLabel(124820, "正在加载中...")
        }
    }
    callBackFun: any;
    callBackFunParams: any;


    @action
    showBatctEdit = (callBackFun?, callBackFunParams?) => {
        this.callBackFun = callBackFun;
        this.callBackFunParams = callBackFunParams;
        this.loading = true;
        API.getBatchModifyField({ pageexpandid: this.pageexpandid }).then(({ error, status, dataList, cells }) => {
            if (status == '1') {
                this.datas = dataList;
                this.cells = cells;
            } else {
                message.error(error);
                throw error;
            }
            this.loading = false;
        })
        this.visible = true;
    }

    onCancel = () => {
        this.visible = false;
    }

    @action
    saveBatctEdit = () => {
        const mythis = this;
        Modal.confirm({
            title: getLabel(125395, '你确定进行批量修改吗?', 'label'),
            okText: getLabel(382958, '确定'),
            okType: 'danger',
            cancelText: getLabel(201, "取消"),
            onOk() {
                mythis.checkDataNull();
            }
        });
    }

    checkDataNull = () => {
        let mess = '';
        this.datas.forEach(element => {
            if (element.fieldvalue == '') {
                mess += element.fieldlabel + ',';
            }
        });
        if (mess.endsWith(',')) mess = mess.substring(0, mess.length - 1);
        if (mess == '') {
            this.doSaveBatchEdit();
        } else {
            const mythis = this;
            Modal.confirm({
                title: mess + getLabel(128103, '的值为空，执行此批量修改时，字段的值将会被清空，请确认是否提交？', 'label'),
                okText: getLabel(382958, '确定'),
                okType: 'danger',
                cancelText: getLabel(201, "取消"),
                onOk() {
                    mythis.doSaveBatchEdit();
                }
            });
        }
    }

    doSaveBatchEdit = () => {
        let s = new Set();
        this.selectedRowKeys.forEach(item => {
            s.add(item);
        });
        this.loading = true;
        let info = "$DepartmentId$,$AllDepartmentId$,$SubcompanyId$,$AllSubcompanyId$"
        this.datas.map((item)=> {//qc:741361 清空的部门分部接口返回0的情况
            if(item.changetype == 2 && info.includes(item.defaultvalue) && item.fieldvalue_r == 0){
                item.fieldvalue_r = '';
                item.fieldvalue = '';
            }
        })
        API.saveBatchModifyField({
            billids: Array.from(s).join(','),
            pageexpandid: this.pageexpandid
            , datas: JSON.stringify(this.datas)
        }).then(({ error, status, errorCode,pageExpendResult }) => {
            if (status == '1' && errorCode == '1') {
                if(pageExpendResult&&pageExpendResult.haveError){
                    showExpendError(pageExpendResult,"/spa/cube/index.html#/main/cube/search?customid="+this.customid+"&");
                    return;
                }
                message.success(getLabel(125585, '成功更新了{0}条数据！', 'label').replace('{0}', this.selectedRowKeys.length));
                if (this.callBackFun) {
                    if (this.callBackFunParams) {
                        this.callBackFun(this.callBackFunParams); //#828123 设置条件必填后，请求接口时需携带这些参数
                    } else {
                        this.callBackFun();
                    }
                }
                this.visible = false;
            } else {
                message.error(error);
                throw error;
            }
            this.loading = false;
        })
    }

    changeRl =(v,fieldId) =>{
        let str = spellResourceCondition(v);
        this.datas.map((field)=> {
            if(field.fieldid == fieldId){
                field.fieldvalue = str;
            }
        })
    }
}